<?php
	if (!session_start()) session_start();
	if (!isset($_SESSION['kiemtradangnhapanan']) || $_SESSION['kiemtradangnhapanan']!=1) {
		header('location: ../../../index.php');
		die();
	}
	$user_dangnhap = $_SESSION["taikhoandangnhapanan"];
	if ($user_dangnhap['idgroup']!="1" && $user_dangnhap['idgroup']!="-1") {
		header('location: index.php');
		die();
	}
	
	$table = "user";
	$check_isset = array(
		'username',
		'password',
		'hoten',
		'diachi',
		'dienthoai',
		'email',
		'idgroup',
		'anhien'
	);
	$check_empty = array(
		'username',
		'password'
	);
	if ($db->check_isset($check_isset)) {
		if ($db->check_empty($check_empty)) {
			for ($i=0; $i<=32; $i++) {
				$d=rand(1,30)%2;
				$salt .= $d ? chr(rand(65,90)) : chr(rand(48,57));
			}
			$hashed = md5($_POST["password"] . $salt);
			$encrypted = $hashed . ':' . $salt;
			if (strlen($_POST["username"])>5) {
				$check_username = $class_user->get_by_username($_POST["username"]);
				if ($check_username != "") {
					?>
						<script language="javascript">alert("Trùng username!");</script>
					<?php
				}else {
					$record = new user;
					$record->dt_set_username($_POST["username"]);
					$record->dt_set_password($encrypted);
					$record->dt_set_hoten($_POST["hoten"]);
					$record->dt_set_diachi($_POST["diachi"]);
					$record->dt_set_dienthoai($_POST["dienthoai"]);
					$record->dt_set_email($_POST["email"]);
					$record->dt_set_idgroup($_POST["idgroup"]);
						$now = getdate();
						$ngay = $now["year"]."-".$now["mon"]."-".$now["mday"]." ".$now["hours"].":".$now["minutes"].":".$now["seconds"];
					$record->dt_set_ngay($ngay);
					$record->dt_set_anhien($_POST["anhien"]);
					
					$record->create();
					?>
						<script language="javascript">alert("Thêm dữ liệu thành công!");</script>
					<?php
					location('index.php?p='.$table.'_quanly');
				}
			}else {
				?>
					<script language="javascript">alert("Username phải có ít nhất 6 ký tự!");</script>
				<?php
			}
		}else {
			?>
				<script language="javascript">alert("Not enough information!");</script>
			<?php
		}
	}
	
?>	
<script type="text/javascript">
function isTest() {
	if (document.getElementById("username").value=="") {
		alert("You have not entered enough information.");
		return false;
	}
	if (document.getElementById("password").value=="") {
		alert("You have not entered enough information.");
		return false;
	}
	
    return true;
}	
</script>			
            	<div class="dangnhap_title">
                <a href="index.php" style="color:#F8991D;" title="Trang chủ">Trang chủ</a>
                <span style="font-size:9px;">>></span> 
                Hệ thống
                <span style="font-size:9px;">>></span> 
                <a href="index.php?p=<?php echo $table; ?>_quanly" style="color:#F8991D;">Thành viên</a> 
            	</div>
            	<form method="post" action="index.php?p=<?php echo $table; ?>_them" onSubmit="return isTest();">
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title" style="font-size:22px; color:#0E6BBB; width: 90%;">
                        Account's Information
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Username(*)
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="username" id="username" onkeypress="return nospace(event);" onkeyup="check_username();" style="width:520px; padding:0px;" />
                    	<span id="username_result"></span>
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Password(*)
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="password" name="password" id="password" style="width:520px; padding:0px;" />
                    </div>
                </div>
                
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title" style="font-size:22px; color:#0E6BBB; width: 90%;">
                        More Information
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Full name:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="hoten" style="width:520px; padding:0px;" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Phone:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="dienthoai" style="width:520px; padding:0px;" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Adress:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="diachi" style="width:520px; padding:0px;" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Email:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="email" style="width:520px; padding:0px;" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Group:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="radio" name="idgroup" value="0" checked="checked" />Manager
                     	<input type="radio" name="idgroup" value="1" />Admin
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Block:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="radio" name="anhien" value="0" checked="checked" />UnBlock
                     	<input type="radio" name="anhien" value="1" />Block
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_input">
                        <input type="image" src="imgs/save_button.png" border="0" />
                        <a href="javascript:history.go(-1);"><img src="imgs/exit_button.png" border="0" /></a>
                    </div>
                </div>
               	</form>